package com.meteor.downloader.videodownloader.ui

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.graphics.RectF
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.meteor.downloader.videodownloader.R
import com.meteor.downloader.videodownloader.mdmGetPxDp

@SuppressLint("ViewConstructor")
class MdmDownloadTipView(
    context: Context,
    private val mDownloadView: View,
) : ConstraintLayout(context) {
    private val mDvsBgPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = context.getColor(R.color.black_50)
        xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
    }
    private val mLocation by lazy {
        IntArray(2)
    }
    private val mRectF by lazy {
        RectF()
    }

    init {
        val layoutParams = LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT
        )
        this.layoutParams = layoutParams
        setLayerType(LAYER_TYPE_SOFTWARE, null)
        setBackgroundResource(R.color.black_50)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        mDownloadView.getLocationOnScreen(mLocation)
        val viewXX = mLocation[0].toFloat()
        val viewYY = mLocation[1].toFloat()
        val viewWidth = mDownloadView.width.toFloat()
        val viewHeight = mDownloadView.height.toFloat()
        val radius = 24f.mdmGetPxDp()
        val space8 = 8f.mdmGetPxDp()

        mRectF.left = viewXX - space8
        mRectF.top = viewYY - space8
        mRectF.right = viewXX + viewWidth + space8
        mRectF.bottom = viewYY + viewHeight + space8

        canvas.drawRoundRect(mRectF, radius, radius, mDvsBgPaint)
    }
}